package hadoop.sort;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;


public class SortJob {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(SortJob.class);
        job.setMapperClass(SortMapper.class);
        job.setReducerClass(SortReducer.class);
        job.setMapOutputKeyClass(SortBean.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(SortBean.class);

        FileInputFormat.setInputPaths(job, new Path("D:\\apache-maven-3.6.3\\hadoopwork\\sort\\input\\text.txt"));
        FileSystem fs = FileSystem.get(conf);
        Path outputpath = new Path("D:\\apache-maven-3.6.3\\hadoopwork\\sort\\output");
        if (fs.exists(outputpath)) {
            fs.delete(outputpath, true);
        }
        FileOutputFormat.setOutputPath(job, outputpath);

        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}
